package com.lj.frame.role;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lj.commons.exception.YerException;
import com.lj.commons.reponse.Result;
import com.lj.frame.role.entity.Role;
import com.lj.frame.role.model.dto.AddRoleDto;
import com.lj.frame.role.model.dto.QueryRoleListParam;
import com.lj.frame.role.service.impl.RoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api
@RequiredArgsConstructor
@Slf4j
@RequestMapping("role")
@RestController
public class RoleController {

    private final RoleServiceImpl roleService;


    @ApiOperation(value = "获取列表")
    @GetMapping("list")
    public Result getList(@Valid QueryRoleListParam queryRoleParam){
        IPage<Role> roleIPage = roleService.queryList(queryRoleParam);
        return Result.ok().setContent("查询成功", roleIPage);
    }

    @GetMapping("info/{id}")
    public Result getList(@PathVariable("id") Integer id){
        Role role = roleService.info(id);
        return Result.ok().setContent("查询成功", role);
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("add")
    public Result add(@Valid @RequestBody AddRoleDto addRoleDto) throws YerException {
        roleService.save(addRoleDto);
        return Result.ok().setContent("添加成功");
    }

    @ApiOperation(value = "更新角色")
    @PostMapping("update")
    public Result update(@Valid @RequestBody AddRoleDto addRoleDto) throws YerException {
        roleService.save(addRoleDto);
        return Result.ok().setContent("更新成功");
    }

    @ApiOperation(value = "删除角色")
    @PostMapping("remove")
    public Result remove(@Valid @RequestBody List<Integer> roleIdList) throws YerException {
        roleService.remove(roleIdList);
        return Result.ok().setContent("删除成功");
    }
}
